//
//  RegisterViewController.m
//  CPICMobileServiceApp
//
//  Created by sinosoft on 13-2-27.
//  Copyright (c) 2013年 lizhiwei. All rights reserved.
//

#import "RegisterViewController.h"
#import "RCAlertViewUtil.h"
#import "ASIHTTPRequest.h"
#import "Config.h"
#import "NSString+SBJson.h"
#import "NSObject+SBJson.h"
#import "UIImageView+WebCache.h"
#import "ASIFormDataRequest.h"

@interface RegisterViewController ()

@end

@implementation RegisterViewController
@synthesize txtUserAccount,txtUserName,txtUserPassword,txtUserIdenPassword,txtIdenCode,txtUserCity;
@synthesize TableViewUser,TableViewPassword;
@synthesize progress;

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        self.title=@"用户注册";
        //self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg640X773@2x"]];
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    NSUserDefaults *accountDefaults = [NSUserDefaults standardUserDefaults];
    NSString *bgimage= [accountDefaults objectForKey:@"UserDefaultBgKindKey"];
    
    if ([bgimage isEqualToString:@"蓝色主题风格"])
    {
        ADD_BACKGROUND_BUTTONITEM(@"icon640X88@2x.png");
        ADD_LEFT_BUTTONITEM(@"back.png");
        ADD_TITLE_NAMEITEM(@"用户注册");
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg640X773@2x"]];
    }
    else if([bgimage isEqualToString:@"棕色主题风格"])
    {
        ADD_BACKGROUND_BUTTONITEM(@"icon640X88_01@2x.png");
        ADD_LEFT_BUTTONITEM(@"back_01.png");
        ADD_TITLE_NAMEITEM(@"用户注册");
        self.view.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"bg640X773_01@2x"]];
    }
    else if([bgimage isEqualToString:@"重大节日皮肤"])
    {
        NSData *dataImg_02=[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_DATA_02];
        NSLog(@"-------找到%@",dataImg_02);
        NSData *dataImg_03=[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_DATA_03];
        NSData *dataImg_04=[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_DATA_04];
        
        NSURL *urlSkinImg_02=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_URL_02]];
        NSURL *urlSkinImg_03=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_URL_03]];
        NSURL *urlSkinImg_04=[NSURL URLWithString:[[NSUserDefaults standardUserDefaults]objectForKey:SKIN_IMG_URL_04]];
        NSLog(@"-------找到333%@",urlSkinImg_02);
        
        UIImageView * LogoImage = [[UIImageView alloc]init];
        [LogoImage setImageWithURL:urlSkinImg_03 success:^(UIImage *image)
         {
             NSData *imgData=UIImagePNGRepresentation(LogoImage.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgData forKey:SKIN_IMG_DATA_03];
         } failure:^(NSError *error)
         {
             [self justShowMessage:@"更新皮肤失败!" title:@"提示"];
         }];
        
        LogoImage.image = [UIImage imageWithData:dataImg_03];
        LogoImage.frame = CGRectMake(0, 0, LogoImage.image.size.width/2, LogoImage.image.size.height/2);
        [self.view addSubview:LogoImage];
        
        //--------------------------
        UIButton * CallOutBtn1 = [UIButton buttonWithType:UIButtonTypeCustom];
        CallOutBtn1.frame = CGRectMake(10, 10, 109/2, 57/2);
        UIImageView * Back = [[UIImageView alloc]init];
        [Back setImageWithURL:urlSkinImg_02 success:^(UIImage *image)
         {
             NSData *imgData=UIImagePNGRepresentation(Back.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgData forKey:SKIN_IMG_DATA_02];
         } failure:^(NSError *error)
         {
             [self justShowMessage:@"更新皮肤失败!" title:@"提示"];
         }];
        
        [Back setImage:[UIImage imageWithData:dataImg_02]];
        [CallOutBtn1 setImage:Back.image forState:UIControlStateNormal];
        [CallOutBtn1 addTarget:self action:@selector(ClickCallOutBtn) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:CallOutBtn1];
        //--------------------------
        
        ADD_TITLE_NAMEITEM(@"用户注册");
        
        UIImageView *bgimage = [[UIImageView alloc]initWithFrame:CGRectMake(0,44, self.view.frame.size.width, self.view.frame.size.height-44)];
        [bgimage setImageWithURL:urlSkinImg_04 success:^(UIImage *image)
         {
             NSData *imgData=UIImagePNGRepresentation(bgimage.image);
             [[NSUserDefaults standardUserDefaults] setObject:imgData forKey:SKIN_IMG_DATA_04];
         } failure:^(NSError *error)
         {
             [self justShowMessage:@"更新皮肤失败!" title:@"提示"];
         }];
        
        [bgimage setImage:[UIImage imageWithData:dataImg_04]];
        [self.view addSubview:bgimage];
        [bgimage release];
    }

    //添加UITableview
    arrUserCity=[[NSMutableArray alloc]initWithObjects:@"深圳市",@"非深圳市", nil];
    TableViewUser = [[UITableView alloc]initWithFrame:CGRectMake(8, 50, 310, 200) style:UITableViewStyleGrouped];
	TableViewUser.delegate = self;
	TableViewUser.dataSource = self;
    
    TableViewUser.opaque = NO;
    TableViewUser.backgroundView = nil;
    TableViewUser.autoresizingMask = UIViewContentModeTop;
    TableViewUser.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    TableViewUser.scrollEnabled=NO;
    TableViewUser.backgroundColor=[UIColor clearColor];
    //[TableViewUser reloadData];
	[self.view addSubview:TableViewUser];
    

    TableViewPassword = [[UITableView alloc]initWithFrame:CGRectMake(8, 220, 310, 250) style:UITableViewStyleGrouped];
	TableViewPassword.delegate = self;
	TableViewPassword.dataSource = self;
    
    TableViewPassword.opaque = NO;
    TableViewPassword.backgroundView = nil;
    TableViewPassword.autoresizingMask = UIViewContentModeTop;
    TableViewPassword.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
    TableViewPassword.scrollEnabled=NO;
    TableViewPassword.backgroundColor=[UIColor clearColor];
    //[TableViewPassword reloadData];
	[self.view addSubview:TableViewPassword];
    
    
    UIButton *btnRegister=[UIButton buttonWithType:UIButtonTypeCustom];
    btnRegister.frame=CGRectMake(90, 410, 310/2, 119/2);
    [btnRegister setBackgroundImage:[UIImage imageNamed:@"reg.png"] forState:UIControlStateNormal];
    [btnRegister addTarget:self action:@selector(RegisterButtonTap) forControlEvents:UIControlEventTouchDown];
    [self.view addSubview:btnRegister];
    UILabel *lblTitle=[[UILabel alloc]initWithFrame:CGRectMake(137, 417, 73, 21)];
    lblTitle.font=[UIFont systemFontOfSize:17.0];
    lblTitle.textColor=[UIColor whiteColor];
    lblTitle.backgroundColor=[UIColor clearColor];
    lblTitle.text=@"我要注册";
    [self.view addSubview:lblTitle];
}
-(void) ClickCallOutBtn
{

    [self.navigationController popViewControllerAnimated:YES];
}
-(void)RegisterButtonTap
{
    if([txtUserAccount.text isEqualToString:@""] || txtUserAccount.text == nil)
    {
        [self justShowMessage:@"用户账号不能为空" title:@"注意"];
        return;
    }
    if ([txtUserPassword.text isEqualToString:@""]  || txtUserPassword.text == nil)
    {
        [self justShowMessage:@"用户登录密码不能为空" title:@"注意"];
        return;
    }
    if (txtUserPassword.text.length<6)
    {
        [self justShowMessage:@"用户登录密码不能小于6位" title:@"注意"];
        return;
    }
    if([txtUserAccount.text isEqualToString:txtUserPassword.text]){
        [self justShowMessage:@"用户密码不能和用户账号相同" title:@"注意"];
        return;
    }
    if (![txtUserPassword.text isEqualToString:txtUserIdenPassword.text]) {
        [self justShowMessage:@"密码不一致" title:@"注意"];
        return;
    }
    if (txtUserCity.text.length==0) {
        [self justShowMessage:@"所属城市不能为空" title:@"注意"];
        return;
    }
    UIAlertView *myAlert=[[UIAlertView alloc]initWithTitle:@"用户协议" message:@"您只有无条件接受以下所有服务条款,才能继续申请注册:1、服务条款的确认和接纳本服务涉及到的太平洋财产保险股份有限公司深圳分公司（以下简称“我司”）产品的所有权以及相关软件的知识产权归我司所有。我司所提供的服务必须按照其发布的公司章程，服务条款和操作规则严格执行。本服务条款的效力范围及于我司的一切产品和服务，用户在享受我司任何单项服务时，应当受本服务条款的约束。用户通过进入注册程序并点击“我接受”的按钮，即表示用户与我司已达成协议，自愿接受本服务条款的所有内容。当用户使用我司单项服务时，用户的使用行为视为其对该单项服务的服务条款以及我司在该单项服务中发出的各类公告的同意。2、服务简介考虑到我司产品服务的重要性，用户同意：（1）提供及时、详尽及准确的个人资料。（2）不断更新注册资料，符合及时、详尽准确的要求。所有原始键入的资料将引用为注册资料。另外，用户可授权我司向第三方透露其注册资料，否则我司不能公开用户的姓名、住址、出件地址、电子邮箱、帐号。除非：（1）用户要求我司或授权某人通过电子邮件服务透露这些信息。（2）相应的法律、法规要求及程序服务需要我司提供用户的个人资料。如果用户提供的资料不准确，不真实，不合法有效，我司保留结束用户使用各项服务的权利。用户在享用各项服务的同时，同意接受提供的各类信息服务。3、服务条款的修改我司有权在必要时修改本服务条款以及各单项服务的相关条款。用户在享受单项服务时，应当及时查阅了解修改的内容，并自觉遵守本服务条款以及该单项服务的相关条款。4、服务修订我司保留随时修改或中断服务而不需通知用户的权利。用户接受我司行使修改或中断服务的权利，我司不需对用户或第三方负责。5、用户隐私制度尊重用户个人隐私是我司的一项基本政策。所以，作为对以上第二点个人注册资料分析的补充，我司一定不会公开、修改或透露用户的注册资料及保存在我司各项服务中的非公开内容，除非我司在诚信的基础上认为透露这些信息在以下几种情况是必要的：（1）遵守有关法律规定，包括在国家有关机关查询时，提供用户在我司的网页上发布的信息内容及其发布时间、互联网地址或者域名。（2）保持维护我司的知识产权和其他重要权利。（3）在紧急情况下竭力维护用户个人和社会大众的隐私安全。（4）根据本条款相关规定或者我司认为必要的其他情况下。6、用户的帐号、密码和安全性您一旦注册成功成为用户，您将得到一个密码和帐号。如果您未保管好自己的帐号和密码而对您、我司或第三方造成的损害，您将负全部责任。另外，每个用户都要对其帐户中的所有活动和事件负全责。您可随时改变您的密码和图标，也可以结束旧的帐户重开一个新帐户。用户同意若发现任何非法使用用户帐号或安全漏洞的情况，立即通告我司。7、拒绝提供担保和免责声明用户明确同意使用我司服务的风险由用户个人承担。我司明确表示不提供任何类型的担保，不论是明确的或隐含的，但是对商业性的隐含担保，特定目的和不违反规定的适当担保除外。我司不担保服务一定能满足用户的要求，也不担保服务不会受中断，对服务的及时性、安全性、真实性、出错发生都不作担保。我司拒绝提供任何担保，包括信息能否准确、及时、顺利地传送。用户理解并接受下载或通过我司产品服务取得的任何信息资料取决于用户自己，并由其承担系统受损、资料丢失以及其它任何风险。我司对在服务网上得到的任何商品购物服务、交易进程、招聘信息，都不作担保。用户不会从我司收到口头或书面的意见或信息，我司也不会在这里作明确担保。8、有限责任我司对直接、间接、偶然、特殊及继起的损害不负责任，这些损害来自：不正当使用产品服务，非法使用服务或用户传送的信息有所变动。这些损害会导致我司形象受损，所以我司早已提出这种损害的可能性。9、禁止服务的商业化用户使用我司各项服务的权利是个人的。用户只能是一个单独的个体而不能是一个公司或实体的商业性组织。用户承诺不经我司同意，不能利用我司各项服务进行销售或其他商业用途。10、保障用户同意保障和维护我司全体成员的利益，负责支付由用户使用超出服务范围引起的律师费用，违反服务条款的损害补偿费用，其它人使用用户的电脑、帐号而产生的费用。用户或者使用用户帐号的其他人在进行游戏过程中侵犯第三方知识产权及其他权利而导致被侵权人索赔的，由用户承担责任。11、结束服务用户或我司可随时根据实际情况中断服务。我司有权单方不经通知终止向用户提供某一项或多项服务；用户有权单方不经通知终止接受我司的服务。结束用户服务后，用户使用我司服务的权利立即终止。从那时起，我司不再对用户承担任何义务。12、通知所有发给用户的通知都可通过电子邮件、常规的信件或在网站显著位置公告的方式进行传送。我司将通过上述方法之一将消息传递给用户，告知他们服务条款的修改、服务变更、或其它重要事情。同时，我司保留对申请了App账户的用户投放商业性广告的权利。13、内容的所有权内容的定义包括：文字、软件、声音、相片、录象、图表；在广告中的全部内容；电子邮件系统的全部内容；我司虚拟社区服务为用户提供的商业信息。所有这些内容均属于我司，并受版权、商标、标签和其它财产所有权法律的保护。所以，用户只能在我司和广告商授权下才能使用这些内容，而不能擅自复制、再造这些内容、或创造与内容有关的派生产品。14、法律用户和我司一致同意有关本协议以及使用我司的服务产生的争议交由仲裁解决，但是我司有权选择采取诉讼方式，并有权选择受理该诉讼的有管辖权的法院。若有任何服务条款与法律相抵触，那这些条款将按尽可能接近的方法重新解析，而其它条款则保持对用户产生法律效力和影响。15、太保移动终端所含服务的信息储存及安全我司对太保移动终端上所有服务将尽力维护其安全性及方便性，但对服务中出现信息删除或储存失败不承担任何负责。另外我们保留判定用户的行为是否符合我司服务条款的要求的权利，如果用户违背了太保移动终端服务条款的规定，将会中断其App服务的帐号。" delegate:self cancelButtonTitle:@"不同意" otherButtonTitles:@"同意", nil];
    myAlert.tag=1;
    [myAlert show];
    [myAlert release];
    //[self setCheckRequest];
}

- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (alertView.tag==1) {
        if (buttonIndex==0) {
        }
        else if (buttonIndex==1)
        {
            [self setCheckRequest];
        }
    }
}
#pragma UITableViewDelegate

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if(tableView==TableViewUser)
    {
        return 3;
    }
    else
    {
        return 3;
    }
}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 50;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if(tableView==TableViewUser)
    {
        static NSString *Identifier=@"Identifier";
        UITableViewCell *cell=(UITableViewCell *)[TableViewUser dequeueReusableCellWithIdentifier:Identifier];
        if (cell==nil)
        {
            cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier]autorelease];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;//选中后颜色没变化
        }
        if(indexPath.row==0)
        {
            UILabel *lblUserAccount=[[UILabel alloc]initWithFrame:CGRectMake(15, 10, 85, 21)];
            lblUserAccount.backgroundColor=[UIColor clearColor];
            lblUserAccount.text=@"用户账号:";
            [cell addSubview:lblUserAccount];
            [lblUserAccount release];
            
            txtUserAccount=[[UITextField alloc]initWithFrame:CGRectMake(100, 10, 197, 31)];
            txtUserAccount.delegate=self;
            txtUserAccount.borderStyle=UITextBorderStyleRoundedRect;
            txtUserAccount.returnKeyType=UIReturnKeyDone;
            txtUserAccount.keyboardType=UIKeyboardTypeASCIICapable;
            [cell addSubview:txtUserAccount];
        }
        else if(indexPath.row==1)
        {
            UILabel *lblUserName=[[UILabel alloc]initWithFrame:CGRectMake(15, 10, 85, 21)];
            lblUserName.backgroundColor=[UIColor clearColor];
            lblUserName.text=@"用户名称:";
            [cell addSubview:lblUserName];
            [lblUserName release];
            
            txtUserName=[[UITextField alloc]initWithFrame:CGRectMake(100, 10, 197, 31)];
            txtUserName.delegate=self;
            txtUserName.borderStyle=UITextBorderStyleRoundedRect;
            txtUserName.returnKeyType=UIReturnKeyDone;
            [cell addSubview:txtUserName];
        }
        else if(indexPath.row==2)
        {
            UILabel *lblUserPassword=[[UILabel alloc]initWithFrame:CGRectMake(15, 10, 85, 21)];
            lblUserPassword.backgroundColor=[UIColor clearColor];
            lblUserPassword.text=@"所属城市";
            [cell addSubview:lblUserPassword];
            [lblUserPassword release];
            
            
            txtUserCity=[[UITextField alloc]initWithFrame:CGRectMake(100, 10, 140, 31)];
            txtUserCity.delegate=self;
            txtUserCity.borderStyle=UITextBorderStyleRoundedRect;
            txtUserCity.returnKeyType=UIReturnKeyDone;
            txtUserCity.enabled=NO;
            txtUserCity.text=@"深圳市";
            
            UIImage *image= [ UIImage imageNamed:@"login_type.png" ];
            UIButton *button = [ UIButton buttonWithType:UIButtonTypeCustom ];
            CGRect frame = CGRectMake( 25.0 , 5.0 , 58/2 , 49/2 );
            button. frame = frame;
            [button setBackgroundImage:image forState:UIControlStateNormal ];
            button. backgroundColor = [UIColor clearColor ];
                
            [button addTarget:self action:@selector(setUserCity) forControlEvents:UIControlEventTouchUpInside];
            cell.accessoryView=button;
            [cell addSubview:txtUserCity];
            
        }
        return cell;
    }
    else
    {
        static NSString *Identifier=@"Identifier";
        UITableViewCell *cell=(UITableViewCell *)[TableViewUser dequeueReusableCellWithIdentifier:Identifier];
        if (cell==nil)
        {
            cell=[[[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:Identifier]autorelease];
            cell.accessoryType = UITableViewCellAccessoryNone;
            cell.selectionStyle=UITableViewCellSelectionStyleNone;
        }
        if(indexPath.row==0)
        {
            UILabel *lblUserPassword=[[UILabel alloc]initWithFrame:CGRectMake(15, 10, 116, 21)];
            lblUserPassword.backgroundColor=[UIColor clearColor];
            lblUserPassword.text=@"用户登录密码:";
            [cell addSubview:lblUserPassword];
            [lblUserPassword release];
            
            txtUserPassword=[[UITextField alloc]initWithFrame:CGRectMake(125, 10, 168, 31)];
            txtUserPassword.delegate=self;
            txtUserPassword.borderStyle=UITextBorderStyleRoundedRect;
            txtUserPassword.returnKeyType=UIReturnKeyDone;
            txtUserPassword.secureTextEntry=YES;
            [cell addSubview:txtUserPassword];
        }
        else if(indexPath.row==1)
        {
            UILabel *lblUserIdenPassword=[[UILabel alloc]initWithFrame:CGRectMake(15, 10, 119, 21)];
            lblUserIdenPassword.backgroundColor=[UIColor clearColor];
            lblUserIdenPassword.text=@"登录密码确认:";
            [cell addSubview:lblUserIdenPassword];
            [lblUserIdenPassword release];
            
            txtUserIdenPassword=[[UITextField alloc]initWithFrame:CGRectMake(125, 10, 168, 30)];
            txtUserIdenPassword.borderStyle=UITextBorderStyleRoundedRect;
            txtUserIdenPassword.delegate=self;
            txtUserIdenPassword.returnKeyType=UIReturnKeyDone;
            txtUserIdenPassword.secureTextEntry=YES;
            [cell addSubview:txtUserIdenPassword];
        }
        else if(indexPath.row==2)
        {
            UIButton * Remindbtn = [UIButton buttonWithType:UIButtonTypeInfoDark];
            Remindbtn.frame = CGRectMake(85, 10, 20, 20);
            [Remindbtn addTarget:self action:@selector(remindclick:) forControlEvents:UIControlEventTouchUpInside];
            [cell addSubview:Remindbtn];
            
            UILabel *lblIdenCode=[[UILabel alloc]initWithFrame:CGRectMake(15, 10, 102, 21)];
            lblIdenCode.backgroundColor=[UIColor clearColor];
            lblIdenCode.text=@"推荐码:";
            [cell addSubview:lblIdenCode];
            [lblIdenCode release];
            
            txtIdenCode=[[UITextField alloc]initWithFrame:CGRectMake(125, 10, 168, 31)];
            txtIdenCode.delegate=self;
            txtIdenCode.borderStyle=UITextBorderStyleRoundedRect;
            txtIdenCode.returnKeyType=UIReturnKeyDone;
            [cell addSubview:txtIdenCode];
        }

        return cell;
    }

    
}
-(void) remindclick:(id)sender
{
    [self justShowMessage:@"填入系统发送给您的短信推荐码,您和推荐人都将获得积分" title:@"提示"];
}

-(void)setUserCity
{
    CustomActionSheet *sheet=[[CustomActionSheet alloc]initWithHeight:284.0f WithSheetTitle:@"" WithDataArray:arrUserCity];
    sheet.tag=1;
    sheet.Customdelegate=self;
    [sheet showInView:self.view];
}
-(void)ClickItem:(NSString *)Title ActionSheetTag:(NSInteger)actionSheetTag
{
    if (actionSheetTag==1) {
        self.txtUserCity.text=Title;
    }
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{

}

- (void)textFieldDidBeginEditing:(UITextField *)textField
{
    if(textField==txtUserPassword)
    {
        [self SetAnimation:100];
    }
    else if(textField==txtUserIdenPassword)
    {
        [self SetAnimation:120];
    }
    else if(textField==txtIdenCode)
    {
        [self SetAnimation:150];
    }
}
- (void)textFieldDidEndEditing:(UITextField *)textField
{
    if(textField==txtUserPassword)
    {
        [self backAnimation:100];
    }
    else if(textField==txtUserIdenPassword)
    {
        [self backAnimation:120];
    }
    else if(textField==txtIdenCode)
    {
        [self backAnimation:150];
    }
}
-(BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string
{
    if(textField==txtUserAccount)
    {
        if(range.location>=50)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    else if(textField==txtUserName)
    {
        if(range.location>=20)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    else if(textField==txtUserPassword)
    {
        if(range.location>=20)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    else if(textField==txtUserIdenPassword)
    {
        if(range.location>=20)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    else if(textField==txtIdenCode)
    {
        if(range.location>=10)
        {
            return NO;
        }
        else
        {
            return YES;
        }
    }
    return YES;
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
    [textField resignFirstResponder];
    return YES;
}

-(void)SetAnimation:(CGFloat)Xorigin
{
    NSTimeInterval animationDuration = 0.30f;
    CGRect frame = self.view.frame;
    //frame.origin.x+=Xorigin;
    frame.origin.y-=Xorigin;
    //frame.size.height +=Xorigin;
    self.view.frame = frame;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame = frame;
    [UIView commitAnimations];
}

-(void)backAnimation:(CGFloat)Xorigin
{
    NSTimeInterval animationDuration = 0.30f;
    CGRect frame = self.view.frame;
    //frame.origin.x -=Xorigin;
    frame.origin.y+=Xorigin;
    //frame.size.height -=Xorigin;
    self.view.frame = frame;
    [UIView beginAnimations:@"ResizeForKeyboard" context:nil];
    [UIView setAnimationDuration:animationDuration];
    self.view.frame = frame;
    [UIView commitAnimations];
    // [textField resignFirstResponder];
}
-(void)setCheckRequest//向太保移动平台发送请求报文
{
//    NSString *strRequestCode=@"RequestCode=60502";
    NSMutableDictionary *dicRequest = [NSMutableDictionary dictionary];
    if(self.txtUserAccount.text.length!=0)
    {
        [dicRequest setObject:self.txtUserAccount.text forKey:@"userAccount"];
    }
    else
    {
        [dicRequest setObject:@"" forKey:@"userAccount"];
    }

    NSString *strRequestJson=[dicRequest JSONRepresentation];
//    strRequestJson=[NSString stringWithFormat:@"&RequestData=%@",strRequestJson];
//    NSString *strUrl=[NSString stringWithFormat:@"%@%@%@",Server_Url,strRequestCode,strRequestJson];
//    NSString *strUrlEncod = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//字符串编码转换
//    NSURL *myurl = [NSURL URLWithString:strUrlEncod];//This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’. Note that ‘%’ escapes are translated via UTF-8.
//    NSLog(@"请求报文为:%@",strUrl);
    ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:Server_Url_Form]];
    
    //设置需要POST的数据，这里提交两个数据，A=a&B=b
    [request setPostValue:@"60502" forKey:@"RequestCode"];
    [request setPostValue:strRequestJson forKey:@"RequestData"];
    
    [request setDelegate:self];
    [request setDidFailSelector:@selector(ResponseCheckFail:)];
    [request setDidFinishSelector:@selector(ResonseCheckSuccess:)];
    [request startAsynchronous];
    
    showProgress = YES;
    if (showProgress) {
        NSLog(@"88888");
        // [NSThread sleepForTimeInterval:1];
        progress= [[MBProgressHUD alloc]initWithView:self.view];
        [self.view addSubview:progress];
        [self.view bringSubviewToFront:progress];
        progress.labelText = @"查询中...";
        [progress show:YES];
        showProgress = NO;
    }
}

- (void)ResonseCheckSuccess:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSString *responseString = [request responseString];
    NSLog(@"responseString %@",responseString);
    NSMutableDictionary *dictResponse=[responseString JSONValue];
    NSLog(@"dictResponse  %@",dictResponse);
    NSMutableDictionary *dictHead=[dictResponse valueForKey:@"head"];
    NSString *strResult= [[NSString alloc] initWithFormat:@"%@",[dictHead objectForKey:@"success"]];
    NSLog(@"strResult%@",strResult);
    if ([strResult isEqualToString:@"1"]) {
        [self setRequest];
    }
    else
    {
        [self justShowMessage:[dictHead valueForKey:@"message"] title:@"注意"];
    }
    [strResult release];
    
    
}

- (void)ResponseCheckFail:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSLog(@"服务器连接失败!");
    [self justShowMessage:@"服务器连接失败" title:@"注意"];
}

-(void)setRequest//向太保移动平台发送请求报文
{
//    NSString *strRequestCode=@"RequestCode=60501";
    NSMutableDictionary *dicRequest = [NSMutableDictionary dictionary];
    if(self.txtUserAccount.text.length!=0)
    {
        [dicRequest setObject:self.txtUserAccount.text forKey:@"userAccount"];
    }
    else
    {
        [dicRequest setObject:@"" forKey:@"userAccount"];
    }
    
    if(self.txtUserName.text.length!=0)
    {
        [dicRequest setObject:self.txtUserName.text forKey:@"userName"];
    }
    else
    {
        [dicRequest setObject:@"" forKey:@"userName"];
    }
    
    if(self.txtUserPassword.text.length!=0)
    {
        [dicRequest setObject:self.txtUserPassword.text forKey:@"userPassword"];
    }
    else
    {
        [dicRequest setObject:@"" forKey:@"userPassword"];
    }
    
    if(self.txtUserIdenPassword.text.length!=0)
    {
        [dicRequest setObject:self.txtUserIdenPassword.text forKey:@"confirmPassword"];
    }
    else
    {
        [dicRequest setObject:@"" forKey:@"confirmPassword"];
    }
    
    if(self.txtIdenCode.text.length!=0)
    {
        [dicRequest setObject:self.txtIdenCode.text forKey:@"authenticationCode"];
    }
    else
    {
        [dicRequest setObject:@"" forKey:@"authenticationCode"];
    }
    
    if ([self.txtUserCity.text isEqualToString:@"深圳市"]) {
        strCityCode=@"UC_00";
    }
    else if([self.txtUserCity.text isEqualToString:@"非深圳市"])
    {
        strCityCode=@"UC_01";
    }
    else
    {
        strCityCode=@"";
    }
    [dicRequest setObject:strCityCode forKey:@"UserCity"];
         
    NSString *strRequestJson=[dicRequest JSONRepresentation];
    //strRequestJson=[NSString stringWithFormat:@"&RequestData=%@",strRequestJson];
//    NSString *strUrl=[NSString stringWithFormat:@"%@%@%@",Server_Url,strRequestCode,strRequestJson];
//    NSString *strUrlEncod = [strUrl stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];//字符串编码转换
//    NSURL *myurl = [NSURL URLWithString:strUrlEncod];//This method expects URLString to contain any necessary percent escape codes, which are ‘:’, ‘/’, ‘%’, ‘#’, ‘;’, and ‘@’. Note that ‘%’ escapes are translated via UTF-8.
//    NSLog(@"请求报文为:%@",strUrl);
    ASIFormDataRequest *request = [[ASIFormDataRequest alloc] initWithURL:[NSURL URLWithString:Server_Url_Form]];
    
    //设置需要POST的数据，这里提交两个数据，A=a&B=b
    [request setPostValue:@"60501" forKey:@"RequestCode"];
    [request setPostValue:strRequestJson forKey:@"RequestData"];
    [request setDelegate:self];
    [request setDidFailSelector:@selector(ResponseFail:)];
    [request setDidFinishSelector:@selector(ResonseSuccess:)];
    [request startAsynchronous];
    
    
    showProgress = YES;
    if (showProgress) {
        NSLog(@"88888");
       // [NSThread sleepForTimeInterval:1];
        progress= [[MBProgressHUD alloc]initWithView:self.view];
        [self.view addSubview:progress];
        [self.view bringSubviewToFront:progress];
        progress.labelText = @"查询中...";
        [progress show:YES];
        showProgress = NO;
    }
}

- (void)ResonseSuccess:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSString *responseString = [request responseString];
    NSLog(@"responseString %@",responseString);
    NSMutableDictionary *dictResponse=[responseString JSONValue];
    NSLog(@"dictResponse  %@",dictResponse);
    NSMutableDictionary *dictHead=[dictResponse valueForKey:@"head"];
    NSString *strResult= [[NSString alloc] initWithFormat:@"%@",[dictHead objectForKey:@"success"]];
    NSLog(@"strResult%@",strResult);
    if ([strResult isEqualToString:@"1"]) {
        [self justShowMessage:@"注册成功" title:nil];
        [self.navigationController popViewControllerAnimated:YES];
    }
    else
    {
        [self justShowMessage:[dictHead objectForKey:@"message"] title:nil];
    }
    [strResult release];
    

}

- (void)ResponseFail:(ASIHTTPRequest *)request
{
    if (!showProgress)
    {
        [progress removeFromSuperview];
        progress = nil;
    }
    NSLog(@"服务器连接失败!");
    [self justShowMessage:@"服务器连接失败" title:@"注意"];
}



- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void)dealloc
{
    [txtUserCity release];
    [txtUserAccount release];
    [txtUserName release];
    [txtUserPassword release];
    [txtUserIdenPassword release];
    [txtIdenCode release];
    [TableViewUser release];
    [TableViewPassword release];
    [super dealloc];
}

@end
